package org.dream.ui.importui;

import java.awt.Color;
import java.util.ArrayList;
import java.util.List;

import org.dream.domain.Stock;

public class ImportStockPanel extends ImportPanel {
	protected List<Stock> holdStocks;

	@Override
	public void onClickImportButton() {
		if (null != holdStocks && holdStocks.size() > 0) {
			persistenceService.saveList(holdStocks);
			holdStocks = null;
			result.setText("导入成功");
		} else {
			result.setText("无可导入数据");
		}
	}

	@Override
	protected void addPromptInfo() {
		StringBuffer stringBuffer = new StringBuffer();

		List<Stock> _exsit_stocks = new ArrayList<Stock>();
		List<Stock> _import_stocks = importService.canOrNotImportStocks(fileText.getText(), _exsit_stocks);
		result.setForeground(Color.BLACK);
		holdStocks = _import_stocks;
		stringBuffer.append("待导入的数据有" + _import_stocks.size() + "个:");
		if (_exsit_stocks.size() > 0) {
			result.setForeground(Color.RED);
			stringBuffer.append("不可导入的数据有" + _exsit_stocks.size() + "个:");

			stringBuffer.append(System.getProperty("line.separator"));
			for (int i = 0; i < _exsit_stocks.size(); i++) {
				Stock stock = _exsit_stocks.get(i);
				stringBuffer.append(i + 1);
				stringBuffer.append(":");
				stringBuffer.append(stock.getCode());
				stringBuffer.append("-");
				stringBuffer.append(stock.getName());
				stringBuffer.append("，");
				stringBuffer.append(System.getProperty("line.separator"));
			}
		}

		result.setText(stringBuffer.toString());
	}

	@Override
	public String getTitle() {
		return "导入股票信息";
	}

	@Override
	public void clearDb() {
		persistenceService.clearDB(Stock.class);
		result.setText("清库成功");

	}
}
